package autoTest.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import autoTest.command.ErrorCode;
import autoTest.command.Params;
import autoTest.database.DataElement;
import autoTest.database.DataManager;
import autoTest.utils.TextUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ReportPart extends RequestPart {
	private static final long serialVersionUID = 2271797150647771297L;
	public ReportPart() {}
    
	protected void doResponse(HttpServletResponse resp, int code, String taskIdStr, int type) throws ServletException, IOException {
		doResponse(resp, code, taskIdStr, null, type);
	}
	
	protected void doResponse(HttpServletResponse resp, int code, String taskIdStr, String task, int type) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");  
		resp.setContentType("text/plain");
		resp.setStatus(HttpServletResponse.SC_OK);
		PrintWriter pWriter = resp.getWriter();
		JSONObject joRet = new JSONObject();
		joRet.put(Params.CODE, code);
		joRet.put(Params.TASK_ID, taskIdStr);
		if (task != null) {
			joRet.put(Params.TASK_ID_DEL, task);
		}
		joRet.put(Params.TYPE, type);
		pWriter.println(joRet.toString());  
	}

	@Override public void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String platformStr = req.getParameter(Params.PLAT_FORM);
    	String deviceIdStr = req.getParameter(Params.DEVICE_ID);
    	String taskIdStr = req.getParameter(Params.TASK_ID);
    	String taskStr = req.getParameter(Params.TASK_ID_DEL);
    	String typeStr = req.getParameter(Params.TYPE);
    	String contentStr = req.getParameter(Params.CONTENT);
    	String codeStr = req.getParameter(Params.CODE);
    	
    	System.out.println("[ReportPart] platformStr:" + platformStr + ",deviceIdStr:" + deviceIdStr + ",taskIdStr:" + taskIdStr
    			+ ",contentStr:" + contentStr + ",codeStr:" + codeStr + ",taskStr:" + taskStr + ",typeStr:" + typeStr);
    	
    	int type = -1;
    	try {
    		type = Integer.valueOf(typeStr);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	if (TextUtils.isEmpty(platformStr) || TextUtils.isEmpty(deviceIdStr) || TextUtils.isEmpty(taskIdStr) || TextUtils.isEmpty(codeStr)) {
    		System.out.println("[ReportPart] params be empty");
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR, taskIdStr, type);
    		return;
    	}

    	if (!(type == 0 || type == 1)) {
    		System.out.println("[ReportPart] type can not parser");
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR, taskIdStr, type);
    		return;
    	}
    	
    	DataElement element = DataManager.getResultElement(taskIdStr);
    	if (element == null) {
    		System.out.println("[ReportPart] no information about " + taskIdStr);
    		doResponse(resp, ErrorCode.ERR_PARAM_ERROR, taskIdStr, type);
    		return;
    	} 
    	
    	System.out.println("[ReportPart] devices before ->" + element.getDeviceSet());
    	Set<String> deviceSet = element.deviceIDStr2DeviceSet(element.getDeviceSet());		//获取所有的设备
    	if (deviceSet.contains(deviceIdStr)) {		//包含对应设备信息
    		JSONObject holder = element.getResultObject();
	    	switch(type) {
	    	case 0:
		    	System.out.println("[ReportPart] report test result");
		    	JSONObject jo = new JSONObject();
		    	jo.put(Params.DEVICE_ID, deviceIdStr);
		    	jo.put(Params.ELE_TEST_RESULT, contentStr);	
		    	deviceSet.remove(deviceIdStr);	//上报结果成功一次，删除标记信号
		    	JSONArray ja = element.getResultObject().getJSONArray(Params.RESULT_ARRAY);
		    	ja.add(jo);
		    	holder.put(Params.TYPE, type);
		    	holder.put(Params.RESULT_ARRAY, ja);
		    	doResponse(resp, ErrorCode.SUCCESS, taskIdStr, type);
		    	break;
		    	
	    	case 1:
	    		System.out.println("[ReportPart] report stop result");
	    		JSONObject jo1 = new JSONObject();
	    		jo1.put(Params.DEVICE_ID, deviceIdStr);			//保存对应设备的停止信息
	    		jo1.put(Params.CODE, codeStr);
	    		deviceSet.remove(deviceIdStr);
		    	JSONArray ja1 = element.getResultObject().getJSONArray(Params.RESULT_ARRAY);
		    	ja1.add(jo1);
		    	holder.put(Params.TYPE, type);
		    	holder.put(Params.RESULT_ARRAY, ja1);
		    	doResponse(resp, ErrorCode.SUCCESS, taskIdStr, taskStr, type);
	    		break;
	    	}
	    	element.setResult(holder.toString());
	    	String tmp = new String("");
	    	for (String tmpDevice : deviceSet) {
	    		tmp = tmp + tmpDevice + ",";
	    	}
	    	System.out.println("[ReportPart] devices tmp ->" + tmp);
	    	if (tmp.length() >= 1) {
	    		element.setDeviceSet(tmp.substring(0, tmp.length() - 1));
	    	} else {
	    		element.setDeviceSet("");
	    	}
	    	System.out.println("[ReportPart] after devices->" + element.getDeviceSet());
	    	DataManager.putResult(element);
	    } else {
    		System.out.println("[ReportPart] no devices");
    		doResponse(resp, ErrorCode.ERR_NO_DEVICE, taskIdStr, type);
    	}
	}
}
